<?php

namespace app\common\model;

use think\Model;

class Equipment extends Model
{
    //
    public function add_one($info){
         $info['display'] = 1;
         $info['inputtime'] =time();
         $info['status'] = 1;

         return $this->insertGetId($info);
    }

    public function get_one($id,$field='*'){
        return $this->where(['display'=>1,'id'=>$id])->find();
    }

    public function edit($id,$info){
        $info['updatetime'] = time();
        return $this->where(['display'=>1,'id'=>$id])->update($info);
    }

    public function del($id){
        return $this->where(['id'=>$id,'display'=>1])->update(['status'=>3]);
    }

    static function getList($page,$pageSize,$search,$order = ['inputtime'=>'desc'],$field=['id','inputtime','name','code','address','status','type']){
        $currentPage = $page;
        $page = ($page - 1) * $pageSize;
        $where = ['code','status','type'];
        $info = self::field($field)->withSearch($where,$search)->order($order)->limit($page,$pageSize)->select();
        $count = self::withSearch($where,$search )->count();
        //todo 创建人名称
        foreach ($info as $infoKey => $infoValue){
            $info[$infoKey]['inputtime'] = date('Y-m-d H:i',$infoValue['inputtime']);
        }
        $arr =  ['list'=>$info,'count'=>$count,'page'=>$currentPage,'pageSize'=>$pageSize];
        return $arr;
    }

    public function searchCodeAttr($query,$value, $data)
    {
        if($value){
            $query->where('code','like', '%'.$value . '%');
        }

    }
    public function searchStatusAttr($query,$value, $data)
    {
        if($value){
            $query->where('status','=', $value );
        }

    }

    public function searchTypeAttr($query,$value, $data)
    {
        if($value){
            $query->where('type','=', $value );
        }

    }

    public function getCode($code){
        return $this->where(['display'=>1,'code'=>$code,'type'=>2])->find();
    }

    public function getType1($ins_id){
        return $this->where(['display'=>1,'ins_id'=>$ins_id,'type'=>1,'status'=>1])->find();
    }

}
